<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="12" :lg="8" :offset="2">
        <a-form-model
          layout="horizontal"
          :label-col="{ span: 5 }"
          :wrapper-col="{ span: 15, offset: 2 }"
          :model="form"
          ref="form"
          :rules="rules"
        >
          <a-form-model-item has-feedback label="原密码：" prop="oldPassword">
            <a-input v-model="form.oldPassword" type="password"></a-input>
          </a-form-model-item>
          <a-form-model-item has-feedback label="新密码：" prop="newPassword">
            <a-input v-model="form.newPassword" type="password"></a-input>
          </a-form-model-item>
          <a-form-model-item has-feedback label="确认密码：" prop="rePassword">
            <a-input v-model="form.rePassword" type="password"></a-input>
          </a-form-model-item>

          <a-form-model-item :wrapper-col="{ span: 14, offset: 5 }">
            <a-button
              type="primary"
              style="margin-left: 25px;"
              @click="onSubmit"
            >
              提交
            </a-button>
          </a-form-model-item>
        </a-form-model>
      </a-col>
      <a-col :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)">
          <a-icon type="cloud-upload-o" class="upload-icon" />
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="userInfo ? userInfo.userFace : option.img" />
        </div>
      </a-col>
    </a-row>

    <avatar-modal ref="modal" @ok="setavatar" />
  </div>
</template>

<script>
import { updatePass, checkOldPass } from "@/api/My";
import AvatarModal from "./AvatarModal";
import Cookies from "js-cookie";

export default {
  components: {
    AvatarModal
  },
  inject: ["reload"],
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.rePassword !== "") {
          this.$refs.form.validateField("rePassword");
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入确认密码"));
      } else if (value !== this.form.newPassword) {
        callback(new Error("二次密码不一致!"));
      } else {
        callback();
      }
    };
    let validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入原密码"));
      } else {
        checkOldPass(value).then(response => {
          let data = response.data;
          if (!data.flag) {
            callback(new Error("原密码不正确"));
          } else {
            callback();
          }
        });
      }
    };
    return {
      userInfo: {},
      // cropper
      preview: {},
      form: {
        oldPassword: "",
        newPassword: "",
        rePassword: ""
      },
      rules: {
        oldPassword: [{ validator: validateOldPass, trigger: "blur" }],
        newPassword: [{ validator: validatePass, trigger: "change" }],
        rePassword: [{ validator: validatePass2, trigger: "change" }]
      },
      option: {
        img: "/avatar2.jpg",
        info: true,
        size: 1,
        outputType: "jpeg",
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      }
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    getUserInfo() {
      this.userInfo = JSON.parse(Cookies.get("userInfo"));
    },
    setavatar(url) {
      this.option.img = url;
      //修改cookie中userInfo的头像信息
      this.userInfo.userFace = url;
      Cookies.set("userInfo", JSON.stringify(this.userInfo));
      this.reload();
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          updatePass(this.form).then(response => {
            let data = response.data;
            console.log(data);
            if (!data.flag) {
              this.$notification["warning"]({
                message: "提示",
                description: data.message
              });
            } else {
              this.$notification["success"]({
                message: `修改成功,请重新登录`
              });
              this.$refs.form.resetFields();
              this.$router.push("/user/login");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.avatar-upload-wrapper {
  height: 200px;
  width: 100%;
}

.ant-upload-preview {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 180px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    top: 0;
    right: 10px;
    font-size: 1.4rem;
    padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }

  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -1rem;
      margin-top: -1rem;
      color: #d6d6d6;
    }
  }

  img,
  .mask {
    width: 100%;
    max-width: 180px;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
  }
}
</style>
